// --------------------------------------------------------------------------------------------------------------------
// <copyright file="EventDispatcher.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.Forge
{
    #region Using Directives

    using System;

    using Org.OpenTrader.Framework.Enums;
    using Org.OpenTrader.Framework.Events;
    using Org.OpenTrader.Framework.Forge.Events;
    using Org.OpenTrader.Framework.Forge.Interfaces;

    #endregion

    /// <summary>
    /// The event dispatcher.
    /// </summary>
    /// <typeparam name="T">
    /// </typeparam>
    public class EventDispatcher<T> : RemotingStub
        where T : DispatchableEventArgs
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="EventDispatcher{T}"/> class.
        /// </summary>
        /// <param name="name">
        /// The name.
        /// </param>
        public EventDispatcher(string name)
        {
            this.Handler = new RemotableEventHandler<T>(name);
        }

        #endregion

        // Handler to distribute pre/post subscribe/unsubscribe events
        #region Events

        /// <summary>
        /// The subscription event cb.
        /// </summary>
        public event EventHandler<SubscriptionEventArgs<T>> SubscriptionEventCB;

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets Handler.
        /// </summary>
        public RemotableEventHandler<T> Handler { get; set; }

        #endregion

        #region Public Methods

        /// <summary>
        /// Dispatch !
        /// </summary>
        /// <param name="sender">
        /// </param>
        /// <param name="args">
        /// </param>
        public void DispatchEvent(object sender, T args)
        {
            this.Handler.RaiseEvent(this, args);
        }

        /// <summary>
        /// Subscribe
        /// </summary>
        /// <param name="wiring">
        /// </param>
        /// <param name="handler">
        /// </param>
        public void Subscribe(IWiring wiring, EventHandler<T> handler)
        {
            // Pre-Subscribe event
            this.PublishSubscriptionEvent(handler, new SubscriptionEventArgs<T>(ESubscriptionEventType.Subscribing, wiring, this.Handler));

            // +=
            this.Handler += handler;

            // Post-Subscribe event
            this.PublishSubscriptionEvent(handler, new SubscriptionEventArgs<T>(ESubscriptionEventType.Subscribed, wiring, this.Handler));
        }

        /// <summary>
        /// Unsubscribe
        /// </summary>
        /// <param name="wiring">
        /// </param>
        /// <param name="handler">
        /// </param>
        public void Unsubscribe(IWiring wiring, EventHandler<T> handler)
        {
            // Pre-UnSubscribe event
            this.PublishSubscriptionEvent(handler, new SubscriptionEventArgs<T>(ESubscriptionEventType.Unsubscribing, wiring, this.Handler));

            // -=
            this.Handler -= handler;

            // Post-UnSubscribe event
            this.PublishSubscriptionEvent(handler, new SubscriptionEventArgs<T>(ESubscriptionEventType.Unsubscribed, wiring, this.Handler));
        }

        #endregion

        #region Methods

        /// <summary>
        /// The publish subscription event.
        /// </summary>
        /// <param name="handler">
        /// The handler.
        /// </param>
        /// <param name="args">
        /// The args.
        /// </param>
        private void PublishSubscriptionEvent(EventHandler<T> handler, SubscriptionEventArgs<T> args)
        {
            // If someone interested
            if (this.SubscriptionEventCB != null)
            {
                // Publish
                this.SubscriptionEventCB(this, args);

                // if args.ForwardToSubscriberArgs has a value after publishing, this means that the consumer of the event wants to pass some data to the
                // new coming handler
                if (args.ForwardToSubscriberArgs != null)
                {
                    foreach (var fargs in args.ForwardToSubscriberArgs)
                    {
                        handler(this, fargs);
                    }
                }
            }
        }

        #endregion
    }
}